package com.project.api.ym.service;


import com.alibaba.fastjson.JSONObject;
import com.project.api.ym.dao.ActivityAttachDao;
import com.project.api.ym.dao.ActivityDao;
import com.project.api.ym.model.Activity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ActivityService {
    @Autowired
    private ActivityDao activityDao;

    @Autowired
    private ActivityAttachDao activityAttachDao;

    @Transactional(rollbackFor=Exception.class)
    public long CreateActivity(JSONObject json,long creator,long updateby) throws Exception {
        long id=activityDao.ActivityCreate(json,creator,updateby);
        long attach_id=activityAttachDao.ActivityAttachCreate(json,id);
        return id;
    }
    @Transactional(rollbackFor=Exception.class)
    public long UpdateActivity(long activity_id,long updateby,JSONObject json){
        long rows=activityDao.ActivityUpdate(activity_id,updateby,json);
        activityAttachDao.ActivityAttachUpdate(activity_id,json);
        return rows;
    }

    public boolean DeleteActivity(ArrayList data){
        ArrayList<String> ids = new ArrayList<>();
        for(int i = 0; i<data.size(); i++){
            ids.add((String) data.get(i));
        }
        return activityDao.ActivityDelete(ids);
    }
    public List GetActivity(Map condition,int offset,int limit,String orderby){
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("title")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
//            where= qs.substring(0,qs.length()-1);
            where=qs;
        }
        if (orderby!=null) where = where + " order by "+orderby;
        where = where + " limit "+ offset + ","+limit;
        return activityDao.GetActivity(where);
    }

    public int GetActivityCount(Map condition){
        Iterator<Map.Entry<String, Object>> entries = condition.entrySet().iterator();
        StringBuilder q = new StringBuilder();
        while(entries.hasNext()){
            Map.Entry<String, Object> entry = entries.next();
            String key = entry.getKey();
            Object value = entry.getValue();
            q.append(" and ");
            if(key.equalsIgnoreCase("stime")||key.equalsIgnoreCase("gtime")){
                q.append("`").append(key).append("`>=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("etime")||key.equalsIgnoreCase("otime")){
                q.append("`").append(key).append("`<=").append("'").append(value).append("'");
            }else if(key.equalsIgnoreCase("title")){
                q.append("`").append(key).append("`").append(" like '%").append(value).append("%'");
            }
            else{
                q.append("`").append(key).append("`=").append("'").append(value).append("'");
            }
        }
        String qs = q.toString();
        String where = "";
        if(!qs.equalsIgnoreCase("")){
            where = qs;
//            where=qs.substring(0,qs.length()-1);
        }
        return activityDao.GetActivityCount(where);
    }

    public boolean UpdateStatus(long id, int status){
        return activityDao.UpdateStatus(id, status);
    }

    public boolean UpdateSend(long id, int status){
        return activityDao.UpdateSend(id, status);
    }

    public boolean Delete(ArrayList<String> ids){
        return activityDao.ActivityDelete(ids);
    }

    public Activity GetActivityById(long id){
        return activityDao.GetActivityById(id);
    }
}
